<?php
/**
 * show
 * show {ProjectID | TodoID}
 * show {ProjectItem | TodoItem}
 * show (projects | todos)
 */

X::import('Application.Modles.Command');

class CShow extends Command {
	/**
	 * @param string $paramString
	 * @return CShow
	 */
	public function parserParamString($paramString=''){
		if ( '' == $paramString ){
			$this->arguments = array();
		}
		else {
			$this->arguments = explode(' ', trim($paramString));
		}
		
		$this->argumentsCount = count($this->arguments);
		
		return $this;
	}
	
	/**
	 * @return CShowResult
	 */
	public function run(){
		if ( XUser::STATUS_OFFLINE == X::app()->user->status ){
			$this->result->errno = XErrorCode::USER_OFFLINE;
			$this->result->error = 'User offline';
			return $this;	
		}

		if ( 0 == $this->argumentsCount ){
			$this->showCurrent();
		}
		else if ( 1 == $this->argumentsCount) {
			if ( is_numeric($this->arguments[0]) ){
				$this->showById();
			}
			else {
				$this->showByNames();
			}
		}
		
		return $this;
	}
	
	/**
	 * Command : show
	 */
	private function showCurrent(){
		if ( X::app()->session->has('CurrentTodo') ){
			$this->result->type = CShowResult::TYPE_TODO;
		} 
		else if ( X::app()->session->has('CurrentProject') ){
			$this->result->type = CShowResult::TYPE_TODO_LIST;
		}
		else {
			$this->result->type = CShowResult::TYPE_PROJECT_LIST;			
		}
		
		$showHandler = 'show'.$this->result->type;
		$this->$showHandler();
	}
	
	/**
	 * show {id}
	 */
	private function showById(){
		$id = $this->arguments[0];
		if ( X::app()->session->has('CurrentProject') ){
			$this->result->type = CShowResult::TYPE_TODO;
		}
		else {
			$this->result->type = CShowResult::TYPE_PROJECT;
		}
		
		$showHandler = 'show'.$this->result->type;
		$this->$showHandler($id);
	}
	
	/**
	 * show projects | todos | {itemName}
	 * */
	private function showByNames(){
		if ( 'projects' == $this->arguments[0] ){
			$this->result->type = CShowResult::TYPE_PROJECT_LIST;
		}
		else if ( 'todos' == $this->arguments[0] ){
			if ( X::app()->session->has('CurrentProject') ){
				$this->result->type = CShowResult::TYPE_TODO_LIST;
			}
			else {
				$this->result->errno = XErrorCode::UNKNOWN_ACTION;
				$this->result->error = 'No project has been selected.';
				return $this;
			}
		}
		else if ( X::app()->session->has('CurrentTodo') ){
			$this->result->type = CShowResult::TYPE_TODO_ATTR;
		} 
		else if ( isset($_SESSION['CurrentProject']) ){
			$this->result->type = CShowResult::TYPE_PROJECT_ATTR;
		}
		else {
			$this->result->errno = XErrorCode::UNKNOWN_ACTION;
			$this->result->error = 'No project has been selected.';
			return $this;
		}
		
		$showHandler = 'show'.$this->result->type;
		$this->$showHandler();
	}
	
	private function selectResult($table, $items=array('*'), $where='1=1'){
		$db = new Database();
		
		$result = $db->SqlBuilder
			->select($items)
			->from(array($table))
			->where($where)
			->execute()
			->allResult();
		$this->result->result = $result;
	}
	
	/**
	 * Get the projects list which belong to current user.
	 */
	private function showProjectList(){
		$this->selectResult('project', array('id', 'name'));
	}
	
	/**
	 * Get the todo list which belong to current project.
	 */
	private function showTodoList(){
		$this->selectResult('todo', array('id', 'subject'), 
			'project='.X::app()->session->CurrentProject
			.' && owner='
			.X::app()->user->account);
	}
	
	/**
	 * Get all the showable information about the todo.
	 * 
	 * @param integer $id 
	 */
	private function showTodo($id = 0){
		$id = (0 === $id) ? X::app()->session->CurrentTodo : $id; 
		$this->selectResult('todo', array('*'), 
			'id='.$id.' && owner='
			.X::app()->user->account);
	}
	
	/**
	 * Get all the showable information about the project.
	 * 
	 * @param integer $id 
	 */
	private function showProject($id){
		$id = (0 === $id) ? X::app()->session->CurrentProject : $id; 
		$this->selectResult('project', array('*'), 
			'id='.$id.' && owner='
			.X::app()->user->account);
	}
	
	/**
	 * Get the attribute about the todo
	 *
	 * @param string $attrName
	 */
	private function showTodoAttr() {
		$this->selectResult('todo', 
			array($this->arguments[0]), 
			'id='.X::app()->session->CurrentTodo.' && owner='.X::app()->user->account);
	}
	
	private function showProjectAttr(){
		$this->selectResult('project', 
			array($this->arguments[0]), 
			'id='.X::app()->session->CurrentProject.' && owner='.X::app()->user->account);
	}
}
?>